﻿using System;
using UnityEditor;

public sealed class ProgressIndicator : IDisposable
{
    private string title;
    private float total = 1f;
    private float progress;
    private double endtime = 0.03;
    private double startime;
    public ProgressIndicator(string title)
    {
        this.title = title;
    }
    public ProgressIndicator(string title, int lenght)
    {
        this.title = title;
        this.total = lenght;
    }
    public void Dispose()
    {
        EditorUtility.ClearProgressBar();
    }

    public void SetTotal(float total)
    {
        this.total = total;
    }
    public void AddProgress(float count = 1f)
    {
        this.progress += count;
    }

    public void SetProgress(float progress)
    {
        this.progress = progress;
    }


    public bool Show(string message)
    {
        double _startime = this.startime;
        this.startime = EditorApplication.timeSinceStartup;
        if (this.startime - _startime <= this.endtime)
        {
            return false;
        }
        float num = this.progress / this.total;
        return EditorUtility.DisplayCancelableProgressBar(this.title, message, num);
    }

    public bool Show(string format, params object[] args)
    {
        return this.Show(string.Format(format, args));
    }
    public bool ShowForce(string message)
    {
        float num = this.progress / this.total;
        return EditorUtility.DisplayCancelableProgressBar(this.title, message, num);
    }
    public bool ShowForce(string format, params object[] args)
    {
        return this.ShowForce(string.Format(format, args));
    }

}
